package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.DriveStep;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.alimaplibrary.MarkerEntity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.transport.DriverLocationVo;
import com.sgb.kjwl.model.entity.transport.OperateGoodsSourcePlaceVO;
import com.sgb.kjwl.model.entity.transport.TransportOrderEntity;
import com.sgb.kjwl.model.entity.transport.common.AddressItem;
import com.sgb.kjwl.view.widget.transport.RealRouteOverlay;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/6/2
 */
public class TrackDetailsDiagramModel extends AndroidViewModel {

    private Context context;

    //装卸点经纬度信息
    private List<LatLonPoint> sourcePoints = new ArrayList<>();
    //司机实时轨迹点信息
    private List<LatLonPoint> realPoints = new ArrayList<>();
    private List<LatLng> realLatLngs = new ArrayList<>();
    private String handleTypeNo;

    private List<DriverLocationVo> locationList = new ArrayList<>();
    private RouteSearch planRouteSearch;
    private RouteSearch realRouteSearch;
    private AMap aMap;

    private List<AddressItem> bundles = new ArrayList<>();
    private List<AddressItem> addressPointList = new ArrayList<>();

    private MapMarkerTools mapMarkerTools;

    public MutableLiveData<Double> realDistanceLD = new MutableLiveData<>();
    public MutableLiveData<Boolean> loadEndLD = new MutableLiveData<>();
    public MutableLiveData<TransportOrderEntity> ldOrderDetails = new MutableLiveData<>();
    public MutableLiveData<Long> ldElapsedTime = new MutableLiveData<>();
    public MutableLiveData<AddressItem> ldCurrAddress = new MutableLiveData<>();
    public MutableLiveData<Boolean> ldComplete = new MutableLiveData<>();

    List<Marker> markers = new ArrayList<>();
    private Marker markerLast;
    private int lastPosition;
    private List<LatLng> latLngs = new ArrayList<>();

    public MapMarkerTools getMapMarkerTools() {
        return mapMarkerTools;
    }

    public void setMapMarkerTools(MapMarkerTools mapMarkerTools) {
        this.mapMarkerTools = mapMarkerTools;
    }

    public TrackDetailsDiagramModel(@NonNull @NotNull Application application) {
        super(application);
        this.context = application;
    }

    /**
     * 请求轨迹点信息
     *
     * @param orderNo
     */
    public void getOrderTrackInfo(String orderNo) {
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getOrderTrail(orderNo).enqueue(new ICallBack<BaseEntity<List<DriverLocationVo>>>() {
            @Override
            public void onSuccess(BaseEntity<List<DriverLocationVo>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.getState() == null || response.getData() == null)
                    return;

                if (response.getState().equals("ok")) {
                    //请求轨迹点成功

                } else {
                    MToast.showToast(context, response.getMsg());
                }
            }
        });
    }

    private TransportOrderEntity orderEntity;

    /**
     * 加载轨迹信息
     *
     * @param orderEntity
     * @param aMap
     */
    public void loadMapInfo(TransportOrderEntity orderEntity, AMap aMap) {
        this.orderEntity = orderEntity;
        this.aMap = aMap;
        aMap.clear();
        latLngs.clear();
        setPlanRouteInfo(orderEntity);
        aMap.setOnMarkerClickListener(marker -> {
            if (marker.getObject() != null) {
                AddressItem item = (AddressItem) marker.getObject();
                for (int i = 0; i < bundles.size(); i++) {
                    if (item.getPosition() == i) {
                        if (bundles.get(i).isBigMarkView()) {
                            setMarkStyle(i, marker, false);
                            ldCurrAddress.postValue(null);
                        } else {
                            setMarkStyle(i, marker, true);
                            ldCurrAddress.postValue(bundles.get(i));
                        }
                        if (lastPosition != i) {
                            resetMarker(false);
                        }
                        markerLast = marker;
                        lastPosition = i;
                        break;
                    }
                }
            }
            return false;
        });
    }

    //设置maker大小图标
    private void setMarkStyle(int pos, Marker marker, boolean isBigMarkView) {
        AddressItem entity = bundles.get(pos);
        BitmapDescriptor bitmapSmall = isBigMarkView ? BitmapDescriptorFactory.fromResource(entity.getBigMakerIcon()) : BitmapDescriptorFactory.fromResource(entity.getMakerIcon());
        entity.setBigMarkView(isBigMarkView);
        marker.setIcon(bitmapSmall);
    }

    public void resetMarker(boolean close) {
        if (markerLast == null)
            return;
        AddressItem entity = bundles.get(lastPosition);
        markerLast.setIcon(BitmapDescriptorFactory.fromResource(entity.getMakerIcon()));
        entity.setBigMarkView(false);
        if (close) {
            markerLast = null;
        }
    }


    /**
     * 预置路径信息
     */
    private void setPlanRouteInfo(TransportOrderEntity orderEntity) {
        if (orderEntity == null)
            return;
        handleTypeNo = orderEntity.getHandleTypeNo();
        //装载 装卸地经纬度
        if (orderEntity.getPlace() != null) {
            OperateGoodsSourcePlaceVO place = orderEntity.getPlace();
            LatLonPoint sendPoint = null;
            if (place.getLatitude() > 0 && place.getLongitude() > 0) {
                sendPoint = new LatLonPoint(place.getLatitude(), place.getLongitude());
            }

            LatLonPoint viaSendPoint = null;
            if (place.getViaSendLatitude() > 0 && place.getViaSendLongitude() > 0) {
                viaSendPoint = new LatLonPoint(place.getViaSendLatitude(), place.getViaSendLongitude());
            }
            LatLonPoint viaDestPoint = null;
            if (place.getViaDestLatitude() > 0 && place.getViaDestLongitude() > 0) {
                viaDestPoint = new LatLonPoint(place.getViaDestLatitude(), place.getViaDestLongitude());
            }

            LatLonPoint destPoint = null;
            if (place.getDestLatitude() > 0 && place.getDestLongitude() > 0) {
                destPoint = new LatLonPoint(place.getDestLatitude(), place.getDestLongitude());
            }

            sourcePoints.clear();
            switch (handleTypeNo) {
                case "11":
                    sourcePoints.add(sendPoint);
                    sourcePoints.add(destPoint);
                    addressPointList.add(new AddressItem("装货点", place.getProvinceName() + "  " + place.getCityName() + "  " + place.getDistrictName(), place.getDetailAddress(), R.mipmap.icon_load_goods, place.getLatitude(), place.getLongitude(), R.mipmap.icon_zhuang, R.mipmap.icon_zhuan_big));
                    addressPointList.add(new AddressItem("卸货点", place.getDestProvinceName() + "  " + place.getDestCityName() + "  " + place.getDestDistrictName(), place.getDestDetailAddress(), R.mipmap.icon_unload_goods, place.getDestLatitude(), place.getDestLongitude(), R.mipmap.icon_xie, R.mipmap.icon_xie_big));
                    break;

                case "12":
                    sourcePoints.add(sendPoint);
                    sourcePoints.add(viaDestPoint);
                    sourcePoints.add(destPoint);
                    addressPointList.add(new AddressItem("装货点", place.getProvinceName() + "  " + place.getCityName() + "  " + place.getDistrictName(), place.getDetailAddress(), R.mipmap.icon_load_goods, place.getLatitude(), place.getLongitude(), R.mipmap.icon_zhuang, R.mipmap.icon_zhuan_big));
                    addressPointList.add(new AddressItem("卸货点1", place.getViaDestProvinceName() + "  " + place.getViaDestCityName() + "  " + place.getViaDestDistrictName(), place.getViaDestDetailAddress(), R.mipmap.icon_unload_one, place.getViaDestLatitude(), place.getViaDestLongitude(), R.mipmap.icon_xie1, R.mipmap.icon_xie1_big));
                    addressPointList.add(new AddressItem("卸货点2", place.getDestProvinceName() + "  " + place.getDestCityName() + "  " + place.getDestDistrictName(), place.getDestDetailAddress(), R.mipmap.icon_unload_two, place.getDestLatitude(), place.getDestLongitude(), R.mipmap.icon_xie2, R.mipmap.icon_xie2_big));
                    break;

                case "21":
                    sourcePoints.add(sendPoint);
                    sourcePoints.add(viaSendPoint);
                    sourcePoints.add(destPoint);
                    addressPointList.add(new AddressItem("装货点1", place.getProvinceName() + "  " + place.getCityName() + "  " + place.getDistrictName(), place.getDetailAddress(), R.mipmap.icon_load_one, place.getLatitude(), place.getLongitude(), R.mipmap.icon_zhuang1, R.mipmap.icon_zhuang1_big));
                    addressPointList.add(new AddressItem("装货点2", place.getViaSendProvinceName() + "  " + place.getViaSendCityName() + "  " + place.getViaSendDistrictName(), place.getViaSendDetailAddress(), R.mipmap.icon_load_two, place.getViaSendLatitude(), place.getViaSendLongitude(), R.mipmap.icon_zhuang2, R.mipmap.icon_zhuang2_big));
                    addressPointList.add(new AddressItem("卸货点", place.getDestProvinceName() + "  " + place.getDestCityName() + "  " + place.getDestDistrictName(), place.getDestDetailAddress(), R.mipmap.icon_unload_goods, place.getDestLatitude(), place.getDestLongitude(), R.mipmap.icon_xie, R.mipmap.icon_xie));
                    break;

                case "22":
                    sourcePoints.add(sendPoint);
                    sourcePoints.add(viaSendPoint);
                    sourcePoints.add(viaDestPoint);
                    sourcePoints.add(destPoint);
                    addressPointList.add(new AddressItem("装货点1", place.getProvinceName() + "  " + place.getCityName() + "  " + place.getDistrictName(), place.getDetailAddress(), R.mipmap.icon_load_one, place.getLatitude(), place.getLongitude(), R.mipmap.icon_zhuang1, R.mipmap.icon_zhuang1_big));
                    addressPointList.add(new AddressItem("装货点2", place.getViaSendProvinceName() + "  " + place.getViaSendCityName() + "  " + place.getViaSendDistrictName(), place.getViaSendDetailAddress(), R.mipmap.icon_load_two, place.getViaSendLatitude(), place.getViaSendLongitude(), R.mipmap.icon_zhuang2, R.mipmap.icon_zhuang2_big));
                    addressPointList.add(new AddressItem("卸货点1", place.getViaDestProvinceName() + "  " + place.getViaDestCityName() + "  " + place.getViaDestDistrictName(), place.getViaDestDetailAddress(), R.mipmap.icon_unload_one, place.getViaDestLatitude(), place.getViaDestLongitude(), R.mipmap.icon_xie1, R.mipmap.icon_xie1_big));
                    addressPointList.add(new AddressItem("卸货点2", place.getDestProvinceName() + "  " + place.getDestCityName() + "  " + place.getDestDistrictName(), place.getDestDetailAddress(), R.mipmap.icon_unload_two, place.getDestLatitude(), place.getDestLongitude(), R.mipmap.icon_xie2, R.mipmap.icon_xie2_big));
                    break;
            }
            planningLoadingRoute(sourcePoints, 1);
        }
    }

    /**
     * 设置实际路线信息
     */
    private void setRealityRouteInfo(TransportOrderEntity orderEntity) {
        if (orderEntity != null && orderEntity.getLocationList() != null) {
            if (orderEntity.getLocationList().size() >= 2) {
                locationList.clear();
                locationList.addAll(orderEntity.getLocationList());
                //计算总里程数
                //添加轨迹点
                realPoints.clear();
                realLatLngs.clear();
                for (DriverLocationVo locationVo : locationList) {
                    if (locationVo.getLatitude() > 0 && locationVo.getLongitude() > 0) {
                        realPoints.add(new LatLonPoint(locationVo.getLatitude(), locationVo.getLongitude()));
                        LatLng latLng = new LatLng(locationVo.getLatitude(), locationVo.getLongitude());
                        realLatLngs.add(latLng);
                        latLngs.add(latLng);
                    }
                }
                long totalTime = Math.abs(locationList.get(0).getTime() - locationList.get(locationList.size() - 1).getTime());

                ldElapsedTime.postValue(totalTime);
                //起始点
                DriverLocationVo locationStart = locationList.get(0);
                addressPointList.add(new AddressItem("实际装货点", locationStart.getProvinceName() + locationStart.getCityName() + locationStart.getDistrictName(), locationStart.getDetailAddress(), R.mipmap.icon_qi, locationStart.getLatitude(), locationStart.getLongitude(), R.mipmap.icon_qi, R.mipmap.icon_qi_big, locationStart.getTime()));
                //截止点
                DriverLocationVo locationEnd = locationList.get(locationList.size() - 1);
                if (orderEntity.getStatus() != null && orderEntity.getStatus() == 3) {
                    //运输中的订单显示司机的实际位置
                    if (orderEntity.getLongitude() > 0 && orderEntity.getLatitude() > 0) {
                        addressPointList.add(new AddressItem("位置", orderEntity.getProvinceName() + orderEntity.getCityName() + orderEntity.getDistrictName(), orderEntity.getDetailAddress(), R.mipmap.icon_sj, orderEntity.getLatitude(), orderEntity.getLongitude(), R.mipmap.icon_sj, R.mipmap.icon_sj_big, locationEnd.getTime()));
                    }
                } else {
                    addressPointList.add(new AddressItem("实际卸货点", locationEnd.getProvinceName() + locationEnd.getCityName() + locationEnd.getDistrictName(), locationEnd.getDetailAddress(), R.mipmap.icon_zhong, locationEnd.getLatitude(), locationEnd.getLongitude(), R.mipmap.icon_zhong, R.mipmap.icon_zhong_big, locationEnd.getTime()));
                }

                aMap.addPolyline(new PolylineOptions()
                                .addAll(realLatLngs)
                                .width(35)
                                .setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.icon_path_green))//纹理
//                    .color(Color.argb(255, 6, 199, 100))//纯色
                );
            } else if (orderEntity.getLocationList().size() == 1) {
                locationList.clear();
                locationList.addAll(orderEntity.getLocationList());
                DriverLocationVo locationStart = locationList.get(0);
                LatLng latLng = new LatLng(locationStart.getLatitude(), locationStart.getLongitude());
                latLngs.add(latLng);
                if (locationStart.getLongitude() > 0 && locationStart.getLatitude() > 0) {
                    addressPointList.add(new AddressItem("实际装货点", locationStart.getProvinceName() + locationStart.getCityName() + locationStart.getDistrictName(), locationStart.getDetailAddress(), R.mipmap.icon_qi, locationStart.getLatitude(), locationStart.getLongitude(), R.mipmap.icon_qi, R.mipmap.icon_qi_big, locationStart.getTime()));
                    addressPointList.add(new AddressItem("位置", locationStart.getProvinceName() + locationStart.getCityName() + locationStart.getDistrictName(), locationStart.getDetailAddress(), R.mipmap.icon_sj, locationStart.getLatitude(), locationStart.getLongitude(), R.mipmap.icon_sj, R.mipmap.icon_sj_big, locationStart.getTime()));
                }
            } else {
                MLog.d("api", "未获取到轨迹点");
            }
//            planningLoadingRoute(realPoints, 2);
        }

        //计算里程
        mileageCounting();
        //添加所有标记点
        addMarkers();
    }

    /**
     * 距离轨迹点计算里程数
     */
    private void mileageCounting() {
        if (locationList == null || locationList.size() < 2) {
            realDistanceLD.postValue(0d);
        } else {
            MLog.d("api", "轨迹点数量" + locationList.size());
            double totalDistance = 0;
            for (int i = 0; i < locationList.size(); i++) {
                if (i + 2 <= locationList.size() && locationList.get(i).getLatitude() > 0 && locationList.get(i).getLongitude() > 0
                        && locationList.get(i + 1).getLatitude() > 0 && locationList.get(i + 1).getLongitude() > 0) {
                    totalDistance += AMapUtils.calculateLineDistance(
                            new LatLng(locationList.get(i).getLatitude(), locationList.get(i).getLongitude()),
                            new LatLng(locationList.get(i + 1).getLatitude(), locationList.get(i + 1).getLongitude()));
                }
            }
            realDistanceLD.postValue(totalDistance);
        }
    }

    private void addMarkers() {
        bundles.clear();
        markers.clear();
        for (int i = 0; i < addressPointList.size(); i++) {
            AddressItem entity = addressPointList.get(i);
            entity.setPosition(i);
            bundles.add(entity);
            LatLng latLng = new LatLng(entity.getLatitude(), entity.getLongitude());
            MarkerEntity markerEntity = new MarkerEntity(latLng, entity.getMakerIcon());
            Marker marker = aMap.addMarker(mapMarkerTools.getMarkerOptions(markerEntity));
            marker.setObject(entity);
            markers.add(marker);
        }

        //显示完整地图路线
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (int i = 0; i < latLngs.size(); i++) {
            builder.include(latLngs.get(i));
        }
        //显示全部路径点,第二个参数是四周留空宽度
        aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));

        loadEndLD.postValue(true);
    }

    /**
     * 添加需要规划的路线数据，并开启异步查询
     *
     * @param points 路线连接点
     * @param type   规划路线的类型：1——计划，2——实际
     */
    public void planningLoadingRoute(List<LatLonPoint> points, int type) {
        //路线点不能少于两个
        if (points == null || points.size() < 2) {
            return;
        }
        List<LatLonPoint> passedByPoints = null;
        //设置开始位置坐标点(注意经纬度不能写反,会报错1800(只能规划到中国区域里的地图路线))
        LatLonPoint startPoint = points.get(0);
        //设置目的地坐标点
        LatLonPoint endPoint = points.get(points.size() - 1);
        //查询信息对象
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(startPoint, endPoint);

        if (points.size() > 2) {
            passedByPoints = new ArrayList<>();
            for (int i = 1; i < points.size() - 1; i++) {
                passedByPoints.add(points.get(i));
            }
        }
        //设置搜索参数 1.fromAndTo 路径的起点终点  2.路径规划的策略（可选），默认为“躲避拥堵” 3.途经点，可选 4.避让区域，可选， 5.货车大小 ,默认轻型车
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(
                fromAndTo,
                RouteSearch.DRIVING_SINGLE_SHORTEST,
                passedByPoints,
                null,
                "");

        if (type == 1) {
            if (planRouteSearch == null)
                planRouteSearch = new RouteSearch(context);
            // 搜索监听
            planRouteSearch.setRouteSearchListener(new OnPlanListener());
            //开始异步查询
            planRouteSearch.calculateDriveRouteAsyn(query);
        } else if (type == 2) {
            if (realRouteSearch == null)
                realRouteSearch = new RouteSearch(context);
            // 搜索监听
            realRouteSearch.setRouteSearchListener(new OnRealListener());
            //开始异步查询
            realRouteSearch.calculateDriveRouteAsyn(query);
        }
    }


    /**
     * 路线规划回调类
     */
    private class OnPlanListener implements RouteSearch.OnRouteSearchListener {

        @Override
        public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

        }

        @Override
        public void onDriveRouteSearched(DriveRouteResult result, int code) {
            if (code == 1000 && result != null && result.getPaths() != null && result.getPaths().size() > 0) {//获取规划路线成功,获取到的是了,路线坐标点的集合
                if (aMap == null)
                    return;
                List<DrivePath> paths = result.getPaths();
                //创建存储坐标点的集合
                List<LatLonPoint> latLonPoints = new ArrayList<>();
                List<LatLng> planLatLngs = new ArrayList<>();

                //遍历获取规划的所有路线坐标点
                for (DriveStep mDriveStep : paths.get(0).getSteps()) {
                    for (LatLonPoint mLatLonPoint : mDriveStep.getPolyline()) {
                        latLonPoints.add(mLatLonPoint);
                        latLngs.add(new LatLng(mLatLonPoint.getLatitude(), mLatLonPoint.getLongitude()));
                        planLatLngs.add(new LatLng(mLatLonPoint.getLatitude(), mLatLonPoint.getLongitude()));
                    }
                }

                //无纹理纯色路线
//                DrivePath drivePath = result.getPaths().get(0);
//                PlanRouteOverlay drivingRouteOverlay = new PlanRouteOverlay(context, aMap, drivePath, result.getStartPos(), result.getTargetPos(), latLonPoints);
//                drivingRouteOverlay.setThroughPointIconVisibility(false);//隐藏途径点
//                drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
//                drivingRouteOverlay.setIsColorfulline(false);//是否用颜色展示交通拥堵情况，默认true
//                drivingRouteOverlay.addToMap();
//                drivingRouteOverlay.zoomToSpan();

                //带纹理纯色路线
                aMap.addPolyline(new PolylineOptions()
                        //路线坐标点的集合
                        .addAll(planLatLngs)
                        //线的宽度
                        .width(35)
                        .setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.icon_path_gray))
                );

                setRealityRouteInfo(orderEntity);


            } else {
                MLog.d("api", "路线规划失败");
            }
        }

        @Override
        public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

        }

        @Override
        public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

        }
    }

    private class OnRealListener implements RouteSearch.OnRouteSearchListener {

        @Override
        public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

        }

        @Override
        public void onDriveRouteSearched(DriveRouteResult result, int code) {
            if (code == 1000 && result != null && result.getPaths() != null && result.getPaths().size() > 0) {//获取规划路线成功,获取到的是了,路线坐标点的集合
                if (aMap == null)
                    return;

                List<DrivePath> paths = result.getPaths();
                //创建存储坐标点的集合
                List<LatLonPoint> latLonPoints = new ArrayList<>();

                double totalDistance = 0;
                //遍历获取规划的所有路线坐标点
                for (DriveStep mDriveStep : paths.get(0).getSteps()) {
                    totalDistance += mDriveStep.getDistance();
                    for (LatLonPoint mLatLonPoint : mDriveStep.getPolyline()) {
                        latLonPoints.add(mLatLonPoint);
                        latLngs.add(new LatLng(mLatLonPoint.getLatitude(), mLatLonPoint.getLongitude()));
                    }
                }

                realDistanceLD.postValue(totalDistance);

                DrivePath drivePath = result.getPaths().get(0);
                RealRouteOverlay overlay = new RealRouteOverlay(context,
                        aMap,
                        drivePath,
                        result.getStartPos(),
                        result.getTargetPos(),
                        latLonPoints);
                overlay.setThroughPointIconVisibility(false);//隐藏途径点
                overlay.setNodeIconVisibility(false);//设置节点marker是否显示
                overlay.setIsColorfulline(false);//是否用颜色展示交通拥堵情况，默认true
                overlay.addToMap();
                overlay.zoomToSpan();

//                //绘制规划路径路线
//                aMap.addPolyline(new PolylineOptions()
//                        //路线坐标点的集合
//                        .addAll(latLngs)
//                        //线的宽度
//                        .width(20)
//                        .color(R.color.color_06C764)
//                );
                //显示完整地图路线
                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for (int i = 0; i < latLngs.size(); i++) {
                    builder.include(latLngs.get(i));
                }
                //显示全部路径点,第二个参数是四周留空宽度
                aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 250));
            } else {
                MLog.d("api", "路线规划失败");
            }
        }

        @Override
        public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

        }

        @Override
        public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

        }
    }

    /**
     * 获取订单详情信息
     */
    public void getDetailData(String mOrderNo) {
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("orderNo", mOrderNo);
        map.put("isGoodsOwner", true);
        NewAPIManager.getInstance().getLogisticsAPI().getShippingOrderDetail(map).enqueue(new Callback<BaseEntity<TransportOrderEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TransportOrderEntity>> call, Response<BaseEntity<TransportOrderEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    ldOrderDetails.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TransportOrderEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
